const express = require('express');
const router = express.Router();

const sequelize = require('../configs/sequelize_config');
const bizQuality = require('../models/quality_model');

router.post('/create', async function(req, res, next) {
  try {
    const { body } = req;

    const result = await bizQuality.create({
      quality_code: body.quality_code,
      domain_type: 1
    });

    res.send({
      result
    });
  } catch (error) {
    res.send(error);
  }
});

router.get('/search', async function(req, res, next) {
  try {
    const result = await bizQuality.findAndCountAll({
      attributes: [
        'quality_id', 'quality_code', 'domain_type',
        'created_at', 'updated_at'
        // [sequelize.Sequelize.fn('date_format', sequelize.Sequelize.col('created_at'), '%Y-%m-%d %H:%i:%s'), 'created_at'],
        // [sequelize.Sequelize.fn('date_format', sequelize.Sequelize.col('updated_at'), '%Y-%m-%d %H:%i:%s'), 'updated_at']
      ],
      order: [
        ['product_code', 'ASC']
      ]
    });

    res.send({
      result
    });
  } catch (error) {
    res.send(error);
  }
});

module.exports = router;
